//
//  KLSGKindRecommendViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/11/1.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGKindRecommendViewController.h"
#import "KLSGCategoryChildTableViewCell.h"
#import "KLSGCategoryTool.h"
#import "KLSGKindModel.h"
#import "KLSGBrifAndVoiceViewController.h"
#import "KLSGDataFail.h"
#import "DDIndicator.h"

@interface KLSGKindRecommendViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *bannerView;

@property (nonatomic, strong) NSMutableArray *titles;
@property (nonatomic, strong) NSMutableArray *banner;
@property (nonatomic, strong) NSMutableDictionary *categoryContents;
@property (nonatomic, strong) KLSGCategoryTool *tool;

@property (nonatomic, strong) KLSGDataFail *failView;
// 加载指示器
@property (nonatomic, strong) DDIndicator *indicator;

@end

@implementation KLSGKindRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self subView];
    [self setupData];
    self.navigationController.navigationBar.translucent = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

//  懒加载
- (KLSGDataFail *)failView
{
    if (!_failView) {
        _failView = [[KLSGDataFail alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _failView;
}

- (KLSGCategoryTool *)tool
{
    if (!_tool) {
        _tool = [[KLSGCategoryTool alloc] init];
    }
    return _tool;
}      

- (void)setupData
{
    // 启动加载指示器
    [self.indicator startAnimating];
    [self.tool setupDataWithUrl:kDetailCategoryUrl(self.Id) block:^(NSDictionary *dic) {
        // 停止指示器
        [self.indicator stopAnimating];
        NSDictionary *focusImagesDic = dic[@"focusImages"];
        self.banner = [NSMutableArray array];
        NSArray *focusImagesArray = focusImagesDic[@"list"];
        for (NSDictionary *focusDic in focusImagesArray) {
            if (![focusDic objectForKey:@"url"]) { // 去广告
                KLSGKindModel *model = [[KLSGKindModel alloc] init];
                [model setValuesForKeysWithDictionary:focusDic];
                [self.banner addObject:model];
            }
        }
        NSDictionary *categoryDic = dic[@"categoryContents"];
        NSArray *categoryArray = categoryDic[@"list"];
        self.titles = [[NSMutableArray alloc] init];
        self.categoryContents = [NSMutableDictionary dictionary];
        for (NSDictionary *tempDic in categoryArray) {
            NSString *title = tempDic[@"title"];
            if (![title isEqualToString:@"排行榜"]) {
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *oneDic in tempDic[@"list"]) {
                    KLSGKindModel *model = [[KLSGKindModel alloc] init];
                    [model setValuesForKeysWithDictionary:oneDic];
                    [array addObject:model];
                }
                [self.titles addObject:title];
                [self.categoryContents setObject:array forKey:title];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self cycleImage];
            [self.tableView reloadData];
        });
    } failDownloadWithBlock:^{
        // 停止指示器
        [self.indicator stopAnimating];
        [self.failView netRequestFailWithBlock:^{
            [self setupData];
            [self.failView hidden];
        }];
        [self.view addSubview:self.failView];
    }];
}

- (void)subView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 114) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 90, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    // 设置总表头
    [self tableHeaderView];
    [self.view addSubview:self.tableView];
    
    // 加载指示器
    self.indicator = [[DDIndicator alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.indicator.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
    [self.view addSubview:self.indicator];
}

// 设置表头
- (void)tableHeaderView
{
    self.bannerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
    self.tableView.tableHeaderView = self.bannerView;
}

// 轮播图
- (void)cycleImage
{
    NSMutableArray *banners = [NSMutableArray array];
    for (KLSGKindModel *model in self.banner) {
        if (model.pic) {
            [banners addObject:model.pic];
        }
    }
    SDCycleScrollView *scrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
    [scrollView setImageURLStringsGroup:banners];
    [self.bannerView addSubview:scrollView];
}

#pragma mark -UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *title = self.titles[section];
    return ((NSArray *)self.categoryContents[title]).count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    KLSGCategoryChildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[KLSGCategoryChildTableViewCell alloc] init];
    }
    NSString *title = self.titles[indexPath.section];
    KLSGKindModel *model = self.categoryContents[title][indexPath.row];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    cell.title.text = model.title;
    cell.detail.text = model.intro;
    cell.playCountLabel.text = [NSString stringWithFormat:@"%@", model.playsCounts];
    cell.albumCountLabel.text = [NSString stringWithFormat:@"%@集", model.tracksCounts];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    headerView.backgroundColor = [UIColor whiteColor];
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 10, 16)];
    icon.image = [UIImage imageNamed:@"finding_title_tag"];
    [headerView addSubview:icon];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, 100, 20)];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [headerView addSubview:titleLabel];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(kScreenWidth - 80, 5, 70, 20);
//    button.backgroundColor = [UIColor whiteColor];
//    [button setTitle:@"更多" forState:UIControlStateNormal];
//    button.titleLabel.font = [UIFont systemFontOfSize:13];
//    button.titleLabel.textAlignment = NSTextAlignmentCenter;
//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button.titleEdgeInsets = UIEdgeInsetsMake(5, -10, 5, 30);
//    [button setImage:[UIImage imageNamed:@"unfold"] forState:UIControlStateNormal];
//    button.imageEdgeInsets = UIEdgeInsetsMake(5, 50, 5, 10);
//    [headerView addSubview:button];
    
    titleLabel.text = self.titles[section];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGBrifAndVoiceViewController *brigAndVoiceVC = [[KLSGBrifAndVoiceViewController alloc] init];
    NSString *title = self.titles[indexPath.section];
    KLSGKindModel *model = self.categoryContents[title][indexPath.row];
    brigAndVoiceVC.albumIdString = [NSString stringWithFormat:@"%@", model.albumId];
    brigAndVoiceVC.model = model;
    [self.navigationController pushViewController:brigAndVoiceVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
